<!-- 物料/产品管理列表 -->
<template>
  <div class="container">
    <div class="content">
      <div>
        <a-space style="display: flex; justify-content: space-between">
          <a-space
            ><a-space>
              <a-input-search
                v-model="numberVal"
                placeholder="请输入物料编码"
                search-button
                @search="search"
              />
            </a-space>
            <a-space>
              <a-input-search
                search-button
                v-model="nameVal"
                placeholder="请输入物料名称"
                @search="search"
              />
            </a-space>
            <a-space>
              <a-button type="outline" @click="reset">
                <template #icon> <icon-refresh /> </template>重置</a-button
              >
            </a-space>
          </a-space>
          <a-space>
            <a-button status="success">
              <template #icon>
                <icon-download />
              </template>
              <template #default>导入</template>
            </a-button>

            <a-button status="warning" @click="getDemandDeriveData">
              <template #icon>
                <icon-export rotate="-90" />
              </template>
              <template #default>导出</template>
            </a-button>
            <a-button type="primary" @click="add">
              <template #icon> <icon-plus /> </template>新增</a-button
            >
          </a-space></a-space
        >
      </div>
    </div>
    <a-divider />
    <div class="content">
      <tableTurn :tableData="getData" ref="tableRef">
        <a-table-column :width="80" align="center">
          <template #title> 序号 </template>
          <template #cell="{ rowIndex }">
            {{ rowIndex + 1 }}
          </template>
        </a-table-column>
        <a-table-column
          align="center"
          :width="200"
          title="物料编码"
          data-index="number"
        >
        </a-table-column>
        <a-table-column
          align="center"
          :width="150"
          title="物料名称"
          data-index="name"
          :ellipsis="true"
          :tooltip="{
            position: 'top',
          }"
        >
        </a-table-column>
        <a-table-column
          align="center"
          :width="150"
          title="规格型号"
          data-index="spec"
          :ellipsis="true"
          :tooltip="{
            position: 'top',
          }"
        >
        </a-table-column>
        <a-table-column
          align="center"
          data-index="basicName"
          :width="100"
          title="基本单位"
        >
        </a-table-column>

        <a-table-column
          align="center"
          :width="100"
          title="物料/产品"
          data-index="materialName"
        >
        </a-table-column>
        <a-table-column align="center" :width="100" title="物料属性">
          <template #cell="{ record }">
            {{
              record.attribute === 1
                ? '自制'
                : record.attribute === 2
                ? '委外'
                : record.attribute === 3
                ? '虚拟'
                : '采购'
            }}
          </template>
        </a-table-column>
        <!-- <a-table-column
          align="center"
          :width="100"
          title="所属分类"
          data-index="spec"
        >
        </a-table-column> -->
        <a-table-column align="center" :width="100" title="是否启用">
          <template #cell="{ record }">
            <div
              :class="record.enable === 1 ? 'state-item-Y' : 'state-item-N'"
              class="state-item"
            >
              {{ record.enable === 1 ? '是' : '否' }}
            </div>
          </template>
        </a-table-column>
        <!-- <a-table-column
          align="center"
          :width="100"
          title="设置安全库存"
          data-index="spec"
        >
        </a-table-column> -->
        <a-table-column align="center" :width="200" title="创建时间">
          <template #cell="{ record }">
            {{ `${dayjs(record.createdAt).format('YYYY-MM-DD hh:mm:ss')}` }}
          </template>
        </a-table-column>
        <a-table-column
          align="center"
          :width="200"
          title="备注"
          data-index="remark"
          :ellipsis="true"
          :tooltip="{
            position: 'top',
          }"
        >
        </a-table-column>
        <a-table-column align="center" :width="150" fixed="right" title="操作">
          <template #cell="{ record }">
            <a-tooltip content="档案">
              <IconFont
                type="icon-details-def"
                :size="26"
                :style="{
                  marginRight: '10px',
                  cursor: 'pointer',
                }"
                @click="goDetail(record)"
              />
            </a-tooltip>
            <a-tooltip content="删除">
              <a-popconfirm
                content="确认删除？"
                type="error"
                @ok="deleteItem(record.id)"
              >
                <IconFont
                  type="icon-delete"
                  :size="26"
                  :style="{
                    marginRight: '10px',
                    cursor: 'pointer',
                  }"
                />
              </a-popconfirm>
            </a-tooltip>
          </template>
        </a-table-column>
      </tableTurn>
    </div>
    <materialAdd
      v-model:visible="modalVisible"
      @materialEmit="materialEmit"
    ></materialAdd>
  </div>
</template>

<script lang="ts" setup>
  import { computed, ref, reactive, onMounted, nextTick } from 'vue';
  import dayjs from 'dayjs';
  import { useRouter } from 'vue-router';
  import {
    GetMaterialList,
    PostMaterialList,
    DelMaterialList,
    DelClassificationList,
  } from '@/api/inventory-management/material-management';
  import mitter from '@/utils/mitt';
  import materialAdd from './components/material-add/index.vue';

  let tableRef = ref();
  let getMaterialId = ref();
  let numberVal = ref(''); // 物料编码搜索值
  let nameVal = ref(''); // 物料编码搜索值
  let router = useRouter();
  mitter.on('MaterialId', (val) => {
    getMaterialId.value = val;
    nextTick(() => {
      tableRef.value.loadData();
    });
  });

  // 获取列表数据
  const getData = async (pageData?: any) => {
    try {
      const data: any = {};
      if (numberVal.value !== '') data.code = numberVal.value;
      if (nameVal.value !== '') data.name = nameVal.value;
      if (getMaterialId.value !== '') data.materialId = getMaterialId.value;
      const res = await GetMaterialList({
        ...pageData,
        ...data,
      });
      if (getMaterialId.value) {
        res.content = res.content.filter(
          (i: any) => i.materialId === getMaterialId.value
        );
      }
      return Promise.resolve(res);
    } catch (error: unknown) {
      console.log(error);
    }
  };
  let modalVisible = ref(false); // 新增弹出框
  let currType = ref(''); // 判断是新增主分类还是子分类
  const currForm = reactive({
    list: {},
  });

  // 点击搜索按钮
  const search = () => {
    tableRef.value.refreshPaging();
    tableRef.value.loadData();
  };

  // 点击重置按钮
  const reset = () => {
    numberVal.value = '';
    nameVal.value = '';
    getMaterialId.value = null;
    mitter.emit('reset', true);
    tableRef.value.refreshPaging();
    tableRef.value.loadData();
  };

  // 监听键盘回车，按了回车也会查询
  const keyDown = (e: any) => {
    if (e.keyCode === 13) {
      tableRef.value.loadData();
    }
  };

  onMounted(() => {
    window.addEventListener('keydown', keyDown); // 监听键盘事件
  });

  // 新增
  const add = () => {
    modalVisible.value = true;
  };

  // 弹出框的自定义事件
  const materialEmit = async (form: any) => {
    try {
      await PostMaterialList(form);
      tableRef.value.loadData();
    } catch (error: unknown) {
      console.log(error);
    }
  };

  // 删除一项
  const deleteItem = async (id: number) => {
    try {
      await DelClassificationList({ id });
      tableRef.value.loadData();
    } catch (error: unknown) {
      console.log(error);
    }
  };

  const goDetail = (record: any) => {
    router.push({ name: 'ProductManageDetails', query: { id: record.id } });
  };
  const isShow = ref(true);
  const showTree = () => {
    isShow.value = !isShow.value;
    mitter.emit('show', isShow.value);
  };
</script>

<style scoped lang="less">
  .container {
    // width: 100%;

    // .content {
    //   padding: 20px 20px;
    // }
    .state-item {
      position: relative;
      left: 50%;
      transform: translateX(-50%);
    }
    .state-item-Y {
      width: 30px;
      height: 30px;
      font-size: 12px;
      border: 1px solid rgba(148, 191, 255, 1);
      background-color: rgba(232, 243, 255, 1);
      border-radius: 5px;
      display: flex;
      align-items: center;
      justify-content: center;
      color: #3d7eff;
    }
    .state-item-N {
      color: #f53f3f;
      width: 30px;
      height: 30px;
      font-size: 12px;
      border: 1px solid rgba(251, 172, 163, 1);
      background-color: rgba(255, 236, 232, 1);
      border-radius: 5px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
</style>
